package com.xxx.json;


import com.google.gson.*;
import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


/**
 * Flying  
 * @author 
 *
 */
public class GsonUtil {  
  
  
private static Gson gson = null;
    static {  
        if (gson == null) {  
            gson = new Gson();
        }  
    }  
  
  
    private GsonUtil() {  
    }  
  
  
    /** 
     * 将object对象转成json字符串 
     *  
     * @param object 
     * @return 
     */  
    public static String GsonString(Object object) {  
        String gsonString = null;  
        if (gson != null) {  
            gsonString = gson.toJson(object);  
        }  
        return gsonString;  
    }



    /** 
     * 将gsonString转成泛型bean 
     *  
     * @param gsonString 
     * @param cls 
     * @return 
     */  
    public static <T> T GsonToBean(String gsonString, Class<T> cls) {  
        T t = null;  
        if (gson != null) {  
            t = gson.fromJson(gsonString, cls);  
        }  
        return t;  
    }  
  
  
   /** 
     * 转成list 
     * 泛型在编译期类型被擦除导致报错 
     * @param gsonString 
     * @param cls 
     * @return 
     */  
    public static <T> List<T> GsonToList(String gsonString, Class<T> cls) {  
        List<T> list = null;  
        if (gson != null) {  
            list = gson.fromJson(gsonString, new TypeToken<List<T>>() {
            }.getType());  
        }  
        return list;  
    }  
  
  
    /** 
     * 转成list 
     * 解决泛型问题 
     * @param json 
     * @param cls 
     * @param <T> 
     * @return 
     */  
    public static <T> List<T> jsonToList(String json, Class<T> cls) {  
        Gson gson = new Gson();
        List<T> list = new ArrayList<T>();  
        JsonArray array = new JsonParser().parse(json).getAsJsonArray();
        for(final JsonElement elem : array){
            list.add(gson.fromJson(elem, cls));  
        }  
        return list;  
    }

    /**
     * 转成list
     * 解决泛型问题
     * @param json
     * @param cls
     * @param <T>
     * @return
     */
    public static <T> List<T> jsonToLinkList(String json, Class<T> cls) {
        Gson gson = new Gson();
        List<T> list = new LinkedList<>();
        JsonArray array = new JsonParser().parse(json).getAsJsonArray();
        for(final JsonElement elem : array){
            list.add(gson.fromJson(elem, cls));
        }
        return list;
    }


    /** 
     * 转成list中有map的 
     *  
     * @param gsonString 
     * @return 
     */  
    public static <T> List<Map<String, T>> GsonToListMaps(String gsonString) {  
        List<Map<String, T>> list = null;  
        if (gson != null) {  
            list = gson.fromJson(gsonString,  
                    new TypeToken<List<Map<String, T>>>() {
                    }.getType());  
        }  
        return list;  
    }  
  
  
    /** 
     * 转成map的 
     *  
     * @param gsonString 
     * @return 
     */  
    public static <T> Map<String, T> GsonToMaps(String gsonString) {
        Map<String, T> map = null;  
        if (gson != null) {  
            map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() {
            }.getType());  
        }  
        return map;  
    }


    /**
     * json字符串转list或者map
     *
     * @param json
     * @param typeToken
     * @return
     */
    public static <T> T fromJson(String json, TypeToken<T> typeToken) {

        Gson gson = new GsonBuilder()
                /**
                 * 重写map的反序列化
                 */
                .registerTypeAdapter(new TypeToken<Map<String, Object>>() {
                }.getType(), new MapTypeAdapter()).create();

        return gson.fromJson(json, typeToken.getType());

    }

    /**
     * json字符串转list或者map
     *
     * @param obj
     * @param obj
     * @param obj
     * @return
     */
    public static <T> T fromObjectToBean(Object obj, Class<T> clazz) {
        String json = GsonString(obj);
        T t = null;
        if (gson != null) {
            t = gson.fromJson(json, clazz);
        }
        return t;
    }

    /**
     * json字符串转list或者map
     *
     * @param obj
     * @param obj
     * @param typeToken
     * @return
     */
    public static <T> T fromJson(Object obj, TypeToken<T> typeToken) {
        String json = GsonString(obj);
        return fromJson(json,typeToken);
    }




    public static class MapTypeAdapter extends TypeAdapter<Object> {

        @Override
        public Object read(JsonReader in) throws IOException {
            JsonToken token = in.peek();
            switch (token) {
                case BEGIN_ARRAY:
                    List<Object> list = new ArrayList<Object>();
                    in.beginArray();
                    while (in.hasNext()) {
                        list.add(read(in));
                    }
                    in.endArray();
                    return list;

                case BEGIN_OBJECT:
                    Map<String, Object> map = new LinkedTreeMap<String, Object>();
                    in.beginObject();
                    while (in.hasNext()) {
                        map.put(in.nextName(), read(in));
                    }
                    in.endObject();
                    return map;

                case STRING:
                    return in.nextString();

                case NUMBER:
                    /**
                     * 改写数字的处理逻辑，将数字值分为整型与浮点型。
                     */
                    double dbNum = in.nextDouble();

                    // 数字超过long的最大值，返回浮点类型
                    if (dbNum > Long.MAX_VALUE) {
                        return dbNum;
                    }

                    // 判断数字是否为整数值
                    long lngNum = (long) dbNum;
                    if (dbNum == lngNum) {
                        return lngNum;
                    } else {
                        return dbNum;
                    }

                case BOOLEAN:
                    return in.nextBoolean();

                case NULL:
                    in.nextNull();
                    return null;

                default:
                    throw new IllegalStateException();
            }
        }

        @Override
        public void write(JsonWriter out, Object value) throws IOException {
            // 序列化无需实现
        }

    }


}  